﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Xna.Framework;

namespace The_Last_Knights.Helper.Xml
{
    public class XmlReader
    {        
        /// <summary>
        /// Doc file XML va tra ve 1 doi tuong tuong ung
        /// </summary>
        /// <param name="FileName">Ten File Xml</param>
        /// <returns></returns>
        public static List<object> Read(string FileName)
        {
            XDocument doc = XDocument.Load(FileName);
            if (doc.Document != null)
            {
                List<object> _data = new List<object>();
                var def = doc.Document.Descendants(XName.Get(@"Element"));
                foreach (var xElement in def)
                {
                    string FakeId = xElement.Attribute(XName.Get(@"ID")).Value;
                    string id = TranslateID(FakeId);
                    string type = xElement.Attribute(XName.Get(@"Type")).Value;
                    int lev = int.Parse(xElement.Attribute(XName.Get(@"Level")).Value);
                    if (lev == 1)
                        _data.Add(Factory.Read(xElement, id, type, lev));    
                }
                return _data;
            }
            return null;
        }

        /// <summary>
        /// Doc ra tat ca cac element co type trung khop voi type da chon
        /// </summary>
        /// <param name="FileName">XML File name</param>
        /// <param name="type">Loai element chon</param>
        /// <param name="level">Level cua element</param>
        /// <returns></returns>
        public static List<XElement> Read(string FileName, string type, int level)
        {
            List<XElement> elements = Read(FileName, type, null);
            foreach (var xElement in elements)
            {
                if (int.Parse(xElement.Attribute(XName.Get(@"Level")).Value) != level)
                    elements.Remove(xElement);            
            }
            return elements;
        }

        /// <summary>
        /// Doc ra cac element co type trung voi type duoc dinh nghia
        /// </summary>
        /// <param name="FileName">XML File name</param>
        /// <param name="type">Loai element chon</param>
        /// <param name="addItem">Must be null</param>
        /// <returns></returns>
        public static List<XElement> Read(string FileName, string type, string addItem)
        {
            XDocument doc = XDocument.Load(FileName);
            if (doc.Document != null)
            {
                List<XElement> _data = new List<XElement>();
                var def = doc.Document.Descendants(XName.Get(@"Element"));
                foreach (var xElement in def)
                {
                    string t = xElement.Attribute(XName.Get(@"Type")).Value;                    
                    if (t == type)
                        _data.Add(xElement);
                }
                return _data;
            }
            return null;
        } 

        /// <summary>
        /// Tim trong file XML element nao co value == value truyen vao thi tra ve
        /// </summary>
        /// <param name="FileName">Ten file XML</param>
        /// <param name="value">Gia tri de so khop</param>
        /// <returns></returns>
        public static object Read(string FileName, string value)
        {
            XDocument doc = XDocument.Load(FileName);
            if (doc.Document != null)
            {
                var def = doc.Document.Descendants(XName.Get(@"Element"));
                foreach (var xElement in def)
                {
                    string val = xElement.Value;
                    if (val == value)
                        return xElement;
                }
                return null;
            }
            return null;
        }

        public static string TranslateID(string name)
        {            
            XElement idElement = (XElement)Read(GameAssertPath.XML_GAMEENTITY_ELEMENT, name);
            if (idElement == null)
                return null;            
            return idElement.Attribute(XName.Get(@"ID")).Value;
        }
    }
}
